// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  // kana:string,
  // 	ciXing:string,
  // 	meaning:Array(string)
  //  audioSrc:string

  var kana = "";
  var isWord = true;
  var jap = "";
  var ciXing = "";
  var meaning = new Array();
  var audioSrc = encodeURI("http://dict.youdao.com/dictvoice?audio=" + event.word + "&le=jap");
  if ((event.word.search(/^[\u3041-\u3096\u30a1-\u30fa\u4e00-\u9fa5ー]+$/) == -1) || event.word == null || event.word == '') {
    return {
      kana: kana,
      jap: jap,
      ciXing: ciXing,
      meaning: meaning,
      audioSrc: audioSrc,
      isWord: false,
      webTrans: new Array(),
      phrase: new Array(),
    }
  }
  const response = await cloud.callFunction({
    // 要调用的云函数名称
    name: 'Request',
    // 传递给云函数的参数
    data: {
      word: event.word,
    }
  });

  var basic = response.result.basic;
  var tempWeb = basic + "标识符" + response.result.web;
  var web_trans = response.result.web;

  //拿到kana
  if (basic.search(/<div id="jc" class="trans-container jc ">/) != -1) {
    var location = basic.search(/<\/h4>/) + 5;
    var len1 = basic.substr(location).search(/<\/h4>/) + 5;
    var len2 = basic.substr(location + len1).search(/<h4>/);

    var regex = /<div id="jc" class="trans-container jc ">[\s\S]*?<h4>(.+)[\s\S]*?(<+?)/;
    kana = regex.exec(basic)[1];
    if (len2 != -1) basic = basic.substr(location, len1 + len2); //去掉后面的释义


    //拿到jap
    if (basic.search(/<span class="origin">/) != -1) {
      regex = /<span class="origin">(.+?)<\/span>/;

      jap = regex.exec(basic)[1];
      while (jap.search("<sup>") != -1) { //去掉所有上标
        var supStart = jap.search(/<sup>/);
        var supEnd = jap.search(/<\/sup>/) + 6;
        jap = jap.substr(0, supStart) + jap.substr(supEnd);
      }
    }


    //拿到ciXing
    if (basic.search(/<div class="col2">.*<span>/) != -1) {
      regex = /<li class="mcols-layout">[\s\S]*?<div class="col2">[\s\S]*?<span>(.+?)<\/span>/;

      ciXing = regex.exec(basic)[1];
      while (ciXing.search("<sup>") != -1) { //去掉所有上标
        var supStart = ciXing.search(/<sup>/);
        var supEnd = ciXing.search(/<\/sup>/) + 6;
        ciXing = ciXing.substr(0, supStart) + ciXing.substr(supEnd);
      }
    }

    //拿到meaning

    //首先去掉用例
    basic = basic.replace(/<li class="secondary mcols-layout">[\s\S]*?<\/li>/g, "");

    //循环拿到meaning
    while (basic.search(/<p>/) != -1) {
      location = basic.search(/<p>/) + 3;
      length = basic.substr(location).search(/<\/p>/);
      var temp = basic.substr(location, length);
      while (temp.search("<sup>") != -1) { //去掉所有上标
        var supStart = temp.search(/<sup>/);
        var supEnd = temp.search(/<\/sup>/) + 6;
        temp = temp.substr(0, supStart) + temp.substr(supEnd);
      }
      meaning.push(temp);
      basic = basic.substr(location);
    }
  }
  //基本释义完成，，接下来网络释义
  var webTrans = new Array();
  var array1 = new Array(); //用来存放日语写法
  var array2 = new Array(); //用来存放网络释义
  var log = new Array();

  var regex = /<span style="cursor: pointer;">[\s\S]*?([\u3041-\u3096\u30a1-\u30fa\u4e00-\u9fa5ー]+)[\s\S]*?<\/span>/g;
  while ((tempR = regex.exec(web_trans)) != null) {
    array1.push(tempR[1])
  }

  regex = /<p class="secondaryFont grey">([\s\S]*?)<\/p>/g;
  var count = 2
  while ((tempR = regex.exec(web_trans)) != null) {
    if (count % 2 == 0) array2.push(tempR[1]);
    count++;
  }

  var tempSize = array1.length;
  for (let i = 0; i < tempSize; i++) {
    webTrans.push("<b>" + array1[i] + "</b><br>" + array2[i]);
    log.push(array1[i] + ": " + array2[i]);
  }
  //截取短语
  var phrase = new Array();


  regex = /<a class="clickable" href="\/dict\?q=.+?">(.+?)<\/a>[\s\S]*?<p>(.+?)<\/p>/g;
  while ((tempR = regex.exec(web_trans)) != null) {
    phrase.push("<b>" + tempR[1] + ": </b>" + tempR[2]);
    log.push(tempR[1] + ": " + tempR[2]);
  }


  return {
    _log: log,
    html: tempWeb,
    kana: kana,
    jap: jap,
    ciXing: ciXing,
    meaning: meaning,
    audioSrc: audioSrc,
    isWord: isWord,
    webTrans: webTrans,
    phrase: phrase,
  }
}